package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.enums.BizDataAuditStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizOprEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.dao.DirectMchntBizInfoMapper;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.exception.RedpException;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 刘玉亮.LL
 */
@Service
public class DirectMchntBizInfoServiceImpl extends ServiceImpl<DirectMchntBizInfoMapper, DirectMchntBizInfo> implements IDirectMchntBizInfoService {

    @Autowired
    private IUpmpOrganizationService organizationService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IUpmpPrivilegeCheckService upmpPrivilegeCheckService;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IBizHisService bizHisService;

    @Autowired
    private IDirectMchntClearInfoService directClearInfoService;
    @Autowired
    private IDirectMchntImageInfoService directMchntImageInfoService;
    @Autowired
    private IDirectMchntBaseInfoService directBaseInfoService;
    @Autowired
    private IDirectMchntSignInfoService directSignInfoService;
    @Override
    public DirectMchntBizInfo getByMchntCd(String mchntCd) {
        LambdaQueryWrapper<DirectMchntBizInfo> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(DirectMchntBizInfo::getMchntCd,mchntCd);
        return this.getOne(wrapper);
    }
    @Override
    @Transactional(value = "transactionManager")
    public OperationResult saveData(String oprMenuId, String oprMenuName, DirectMchntBizInfo entity, HttpServletRequest request, String webNextUrl, String flag) throws RedpException {
        Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        UpmpOrganization org = organizationService.getOrgByCode(user.getOrgCode(), user.getInstId());

        if (Common.isBlank(entity.getMchntCd())) {
            log.error("请求参数(MchntCd)异常!");
            return OperationResult.buildFailureResult("请求参数(MchntCd)异常!", result);
        }

        entity.setInstId(user.getInstId());
        //
        String status = "";
        String auditStatus = "";
        boolean saveTmpFlag = false;
        boolean checkPass = false;
        //
        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            UpmpPrivilegeCheck privilegeCheck = upmpPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "银联直连商户管理:新增");
            // 重复新增，变为修改操作
            if (StringUtils.isNotBlank(entity.getMchntCd())) {
                DirectMchntBizInfo byMchntCd = this.getByMchntCd(entity.getMchntCd());
                if (null != byMchntCd) {
                    entity.setId(byMchntCd.getId());
                } else {
                    entity.setId(null);
                }
            } else {
                entity.setId(null);
            }
            //
            entity.setOprId(user.getId());
            entity.setCrtOrgId(org.getOrgCode());
            entity.setCreateTime(lastUpddt);
            entity.setLastOprId(user.getId());
            entity.setLastUpdateTime(lastUpddt);
            //
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                status = BizDataStatusEnum.STATUS_DRAFT.getType();
                auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType();
                entity.setMchntBizStatus(status);
                entity.setAuditStatus(auditStatus);
                this.save(entity);

                saveTmpFlag = true;
                bizTmpService.saveBizTmpDtl(entity, user, org.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt
                        , oprMenuId, oprMenuName, entity.getMchntBizStatus(), entity.getAuditStatus(), entity.getId(), webNextUrl, "", "", entity.getMchntCd(), null, null);
            } else {
                status = BizDataStatusEnum.STATUS_NORMAL.getType();
                auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType();
                entity.setMchntBizStatus(status);
                entity.setAuditStatus(auditStatus);
                this.save(entity);

                bizHisService.saveBizHisNew(entity, entity, user, org.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt
                        , oprMenuId, oprMenuName, entity.getId(), entity.getMchntCd(), null, null);
            }
            result.put("id", entity.getId());
            result.put("mchntCd", entity.getMchntCd());
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {
            // 上一次操作如果是新增-待提交审批的，前端传来upd标志，转到add方法处理
            DirectMchntBaseInfo dbDirectMchntBaseInfo = directBaseInfoService.getByMchntCd(entity.getMchntCd());
            if (dbDirectMchntBaseInfo.getAuditStatus().equals(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_NOTCOMMIT.getType())) {
                //
                return this.saveData(oprMenuId, oprMenuName, entity, request, webNextUrl, "add");
            }
            //
            String id = request.getParameter("id");
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }

            UpmpPrivilegeCheck privilegeCheck = upmpPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "银联直连商户管理:修改");
            DirectMchntBizInfo dbDirectMchntBizInfo = this.getById(id);
            DirectMchntBizInfo directMchntBizInfo = new DirectMchntBizInfo();
            BeanUtils.copyProperties(entity, directMchntBizInfo);

            entity.setLastOprId(user.getId());
            entity.setLastUpdateTime(lastUpddt);
            entity.setMchntBizStatus(dbDirectMchntBizInfo.getMchntBizStatus());

            status = dbDirectMchntBizInfo.getMchntBizStatus();

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType();

                dbDirectMchntBizInfo.setLastOprId(user.getId());
                dbDirectMchntBizInfo.setLastUpdateTime(lastUpddt);
                dbDirectMchntBizInfo.setAuditStatus(auditStatus);

                this.updateById(dbDirectMchntBizInfo);

                //检查临时表是否有数据，如果没用则新增
                entity.setAuditStatus(auditStatus);
                directMchntBizInfo = new DirectMchntBizInfo();
                BeanUtils.copyProperties(dbDirectMchntBizInfo, directMchntBizInfo);
                BeanUtils.copyProperties(entity, directMchntBizInfo);

                saveTmpFlag = true;
                bizTmpService.saveBizTmpDtl(directMchntBizInfo, user, org.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt
                        , oprMenuId, oprMenuName, directMchntBizInfo.getMchntBizStatus(), directMchntBizInfo.getAuditStatus(), directMchntBizInfo.getId(), webNextUrl, "", "", directMchntBizInfo.getMchntCd(), null, null);
            } else {
                auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType();

                entity.setAuditStatus(auditStatus);

                BeanUtils.copyProperties(entity, directMchntBizInfo);

                this.updateById(directMchntBizInfo);

                //历史表新增
                bizHisService.saveBizHisNew(directMchntBizInfo, dbDirectMchntBizInfo, user, org.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt
                        , oprMenuId, oprMenuName, directMchntBizInfo.getId(), directMchntBizInfo.getMchntCd(), null, null);
            }
            result.put("id", dbDirectMchntBizInfo.getId());
            result.put("mchntCd", dbDirectMchntBizInfo.getMchntCd());
        } else if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {
            String id = request.getParameter("id");
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }
            DirectMchntBizInfo dbDirectMchntBizInfo = this.getById(id);
            if (Common.isBlank(dbDirectMchntBizInfo)) {
                log.error("根据id没有找到指定数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定数据！", result);
            }

            DirectMchntBizInfo directMchntBizInfo = new DirectMchntBizInfo();
            BeanUtils.copyProperties(dbDirectMchntBizInfo, directMchntBizInfo);

            UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
            String bizOpr = "";
            if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)) {
                bizOpr = BizOprEnum.OPR_STOP.getType();
                privilegeCheck = upmpPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "银联直连商户管理:停用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                }
            } else if (PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)) {

                bizOpr = BizOprEnum.OPR_USE.getType();
                privilegeCheck = upmpPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "银联直连商户管理:启用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                }
            } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {

                bizOpr = BizOprEnum.OPR_CANCEL.getType();
                privilegeCheck = upmpPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "银联直连商户管理:注销");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                }
            }
            directMchntBizInfo.setLastOprId(user.getId());
            directMchntBizInfo.setLastUpdateTime(lastUpddt);

            status = directMchntBizInfo.getMchntBizStatus();
            auditStatus = directMchntBizInfo.getAuditStatus();

            this.updateById(directMchntBizInfo);

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                saveTmpFlag = true;
                //检查临时表是否有数据，如果没用则新增
                bizTmpService.saveBizTmpDtl(directMchntBizInfo, user, org.getInstId(), bizOpr, lastUpddt
                        , oprMenuId, oprMenuName, directMchntBizInfo.getMchntBizStatus(), directMchntBizInfo.getAuditStatus(), directMchntBizInfo.getId(), webNextUrl, "", "", directMchntBizInfo.getMchntCd(), null, null);
            } else {
                //历史表新增
                bizHisService.saveBizHisNew(directMchntBizInfo, dbDirectMchntBizInfo, user, org.getInstId(), bizOpr, lastUpddt
                        , oprMenuId, oprMenuName, directMchntBizInfo.getId(), directMchntBizInfo.getMchntCd(), null, null);
            }
            result.put("id", directMchntBizInfo.getId());
            result.put("mchntCd", directMchntBizInfo.getMchntCd());
        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {
            String id = request.getParameter("id");
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }

            //获取临时表数据并转换成实体对象
            BizTmp tmp = bizTmpService.getById(id);
            if (Common.isBlank(tmp)) {
                log.error("根据id没有找到指定临时表数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定临时表数据！", result);
            }
            String bizdata = "";
            if (null != tmp.getBizData1()) {
                bizdata += tmp.getBizData1();
            }
            if (null != tmp.getBizData2()) {
                bizdata += tmp.getBizData2();
            }
            if (null != tmp.getBizData3()) {
                bizdata += tmp.getBizData3();
            }
            // 临时表数据
            DirectMchntBizInfo directMchntBizInfoNew = JSON.parseObject(bizdata, DirectMchntBizInfo.class);
            // 正式表数据
            DirectMchntBizInfo directMchntBizInfo = this.getById(id);
            // 备份正式表数据
            DirectMchntBizInfo directMchntBizInfoOld = new DirectMchntBizInfo();
            BeanUtils.copyProperties(directMchntBizInfo, directMchntBizInfoOld);

            if (Common.isBlank(directMchntBizInfo) && !BizOprEnum.OPR_ADD.equals(tmp.getBizOpr())) {
                log.error("根据id没有找到指定数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定数据！", result);
            }

            String opt = request.getParameter("opt");
            if (Common.isBlank(opt)) {
                log.error("请求参数(opt)异常!");
                return OperationResult.buildFailureResult("请求参数(opt)异常!", result);
            }
            //
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                // 审批通过将临时表数据更新到正式表
                BeanUtils.copyProperties(directMchntBizInfoNew, directMchntBizInfo);
            } else {
                String approvalRefuseReason = request.getParameter("approvalRefuseReason");
                directMchntBizInfo.setAuditRefuseReason(approvalRefuseReason);
            }

            String bizOpr = "";
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(directMchntBizInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_ADD.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(directMchntBizInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_CANCEL.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(directMchntBizInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_STOP.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(directMchntBizInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_USE.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(directMchntBizInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                    directMchntBizInfo.setMchntBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                } else {
                    directMchntBizInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_UPDATE.getType();
            }
            status = directMchntBizInfo.getMchntBizStatus();
            auditStatus = directMchntBizInfo.getAuditStatus();

            // 状态赋值完成后，入库正式表
            this.updateById(directMchntBizInfo);
            //
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                checkPass = true;
                //审批通过将正式表更新到历史表
                bizHisService.saveBizHisNew(directMchntBizInfo, directMchntBizInfoOld, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, directMchntBizInfo.getId(), directMchntBizInfo.getMchntCd(), null, null);
            }
            saveTmpFlag = true;
            // 修改临时表的审核状态等字段
            bizTmpService.saveBizTmpDtl(directMchntBizInfo, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, directMchntBizInfo.getMchntBizStatus(), directMchntBizInfo.getAuditStatus(), directMchntBizInfo.getId(), webNextUrl, "", "", directMchntBizInfo.getMchntCd(), null, null);
            result.put("id", directMchntBizInfo.getId());
            result.put("mchntCd", directMchntBizInfo.getMchntCd());
        }
        if (Common.isBlank(status) || Common.isBlank(auditStatus)) {
            log.error("业务信息同步状态时出错！状态异常，请联系管理员");
            return OperationResult.buildFailureResult("业务信息同步状态时出错！状态异常，请联系管理员", result);
        }
        // 业务信息保存后，同步基本信息，签约信息，影像信息，清算信息状态
        this.synchState(saveTmpFlag, checkPass, status, auditStatus, entity.getMchntCd());
        //
        result.put("formNm", "baseInfo");

        return OperationResult.buildSuccessResult("数据保存处理完成", result);
    }
    /**
     * 业务信息保存后，同步基本信息，签约信息，影像信息，清算信息状态
     *
     * @param saveTmpFlag 保存到临时表标志
     * @param checkPass   审核通过标志
     * @param status      业务状态
     * @param auditStatus 审核状态
     * @param mchntCd     商户号
     */
    private void synchState(boolean saveTmpFlag, boolean checkPass, String status, String auditStatus, String mchntCd) throws RedpException {
        // 商户基本信息表
        DirectMchntBaseInfo directBaseInfo = directBaseInfoService.getByMchntCd(mchntCd);
        directBaseInfo.setMchntStatus(status);
        directBaseInfo.setAuditStatus(auditStatus);
        // 新增/修改审核通过后，修改银联报备状态为 未报备
        if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType().equals(auditStatus) ||
                BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType().equals(auditStatus)) {
            // 00-未报备 01-报备中 10-已系统报备 11-系统报备失败 20-已手工报备 21-手工报备失败
            directBaseInfo.setCupsReportStatus("00");
        }
        directBaseInfoService.save(directBaseInfo);
        // 商户签约信息表
        DirectMchntSignInfo directSignInfo = directSignInfoService.getByMchntCd(mchntCd);
        directSignInfo.setMchntSignStatus(status);
        directSignInfo.setAuditStatus(auditStatus);
        directSignInfoService.save(directSignInfo);
        // 商户影像信息表
        List<UpmpBMchntImageInfo> bMchntImageInfos = directMchntImageInfoService.getBMchntImageInfoByMchntNo(mchntCd);
        for (UpmpBMchntImageInfo bMchntImageInfo : bMchntImageInfos) {
            bMchntImageInfo.setAuditStatus(auditStatus);
        }
        directMchntImageInfoService.updateBatchById(bMchntImageInfos);
        // 商户清算信息表
        DirectMchntClearInfo directClearInfo = directClearInfoService.getByMchntCd(mchntCd);
        directClearInfo.setMchntClearStatus(status);
        directClearInfo.setAuditStatus(auditStatus);
        directClearInfoService.save(directClearInfo);
        //
        if (saveTmpFlag) {
            // 保存临时表，需要同步临时表数据

            // 根据商户号获取临时表数据
            Map<String, List<Object>> tableDataMap = new HashMap<>();
            Map<String, List<BizTmp>> bizTmpDataMap = new HashMap<>();
            List<BizTmp> bizTmpList = bizTmpService.getBizTmpByMchntCd(mchntCd);
            // 修改临时表数据的状态
            for (BizTmp bizTmp : bizTmpList) {
                String oprTableEname = bizTmp.getOprTableEname().toUpperCase();
                if (!tableDataMap.containsKey(oprTableEname)) {
                    tableDataMap.put(oprTableEname, new ArrayList<>());
                }
                if (!bizTmpDataMap.containsKey(oprTableEname)) {
                    bizTmpDataMap.put(oprTableEname, new ArrayList<>());
                }
                bizTmpDataMap.get(oprTableEname).add(bizTmp);
                // 赋值状态
                bizTmp.setBizStatus(status);
                bizTmp.setAuditStatus(auditStatus);
                String bizData = (StringUtils.isBlank(bizTmp.getBizData1()) ? "" : bizTmp.getBizData1()) +
                        (StringUtils.isBlank(bizTmp.getBizData2()) ? "" : bizTmp.getBizData2()) +
                        (StringUtils.isBlank(bizTmp.getBizData3()) ? "" : bizTmp.getBizData3());
                // 取值
                switch (oprTableEname) {
                    case "T_B_DIRECT_MCHNT_BASE_INFO":
                        DirectMchntBaseInfo directBaseInfoTmp = JSON.parseObject(bizData, DirectMchntBaseInfo.class);
                        directBaseInfoTmp.setMchntStatus(status);
                        directBaseInfoTmp.setAuditStatus(auditStatus);
                        bizData = JSON.toJSONString(directBaseInfoTmp);
                        // 添加
                        tableDataMap.get(oprTableEname).add(directBaseInfoTmp);
                        break;
                    case "T_B_DIRECT_MCHNT_SIGN_INFO":
                        DirectMchntSignInfo directSignInfoTmp = JSON.parseObject(bizData, DirectMchntSignInfo.class);
                        directSignInfoTmp.setMchntSignStatus(status);
                        directSignInfoTmp.setAuditStatus(auditStatus);
                        bizData = JSON.toJSONString(directSignInfoTmp);
                        // 添加
                        tableDataMap.get(oprTableEname).add(directSignInfoTmp);
                        break;
                    case "T_B_MCHNT_IMAGE_INFO":
                        UpmpBMchntImageInfo bMchntImageInfoTmp = JSON.parseObject(bizData, UpmpBMchntImageInfo.class);
                        bMchntImageInfoTmp.setAuditStatus(auditStatus);
                        bizData = JSON.toJSONString(bMchntImageInfoTmp);
                        // 添加
                        tableDataMap.get(oprTableEname).add(bMchntImageInfoTmp);
                        break;
                    case "T_B_DIRECT_MCHNT_CLEAR_INFO":
                        DirectMchntClearInfo directClearInfoTmp = JSON.parseObject(bizData, DirectMchntClearInfo.class);
                        directClearInfoTmp.setMchntClearStatus(status);
                        directClearInfoTmp.setAuditStatus(auditStatus);
                        bizData = JSON.toJSONString(directClearInfoTmp);
                        // 添加
                        tableDataMap.get(oprTableEname).add(directClearInfoTmp);
                        break;
                }
                // 还原
                String bizData1;
                String bizData2 = "";
                String bizData3 = "";
                if (bizData.length() > 4000) {
                    bizData1 = bizData.substring(0, 3999);
                    bizData2 = bizData.substring(4000);
                } else {
                    bizData1 = bizData;
                }
                if (bizData2.length() > 4000) {
                    bizData2 = bizData2.substring(0, 3999);
                    bizData3 = bizData.substring(8000);
                }
                bizTmp.setBizData1(bizData1);
                bizTmp.setBizData2(bizData2);
                bizTmp.setBizData3(bizData3);
            }
            // 修改临时表数据
            bizTmpService.updateBatchById(bizTmpList);
            // 审核通过，将正式表数据更新到历史表
            if (checkPass) {
                String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
                UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
                UpmpOrganization org = organizationService.getOrgByCode(user.getOrgCode(), user.getInstId());
                // 商户基本信息
                List<Object> directBaseInfoTmps = tableDataMap.get("T_B_DIRECT_MCHNT_BASE_INFO");
                List<BizTmp> directBaseInfoBizTmps = bizTmpDataMap.get("T_B_DIRECT_MCHNT_BASE_INFO");
                DirectMchntBaseInfo directBaseInfoTmp = (DirectMchntBaseInfo) directBaseInfoTmps.get(0);
                BizTmp directBaseInfoBizTmp = directBaseInfoBizTmps.get(0);
                // 审核通过，将临时表数据同步到正式表
                directBaseInfoTmp.setOprId(directBaseInfo.getOprId());
                directBaseInfoTmp.setCrtOrgId(directBaseInfo.getCrtOrgId());
                directBaseInfoTmp.setCreateTime(directBaseInfo.getCreateTime());
                directBaseInfoService.save(directBaseInfoTmp);
                // 将正式表数据更新到历史表
                bizHisService.saveBizHisNew(directBaseInfoTmp, directBaseInfo, user, org.getInstId(), directBaseInfoBizTmp.getBizOpr(), lastUpddt, directBaseInfoBizTmp.getOprMenuId(), directBaseInfoBizTmp.getOprMenuName(), directBaseInfoBizTmp.getRealId(), mchntCd, null, null);
                // 商户签约信息
                List<Object> directSignInfoTmps = tableDataMap.get("T_B_DIRECT_MCHNT_SIGN_INFO");
                List<BizTmp> directSignInfoBizTmps = bizTmpDataMap.get("T_B_DIRECT_MCHNT_SIGN_INFO");
                DirectMchntSignInfo directSignInfoTmp = (DirectMchntSignInfo) directSignInfoTmps.get(0);
                BizTmp directSignInfoBizTmp = directSignInfoBizTmps.get(0);
                // 审核通过，将临时表数据同步到正式表
                directSignInfoTmp.setOprId(directBaseInfo.getOprId());
                directSignInfoTmp.setCrtOrgId(directBaseInfo.getCrtOrgId());
                directSignInfoTmp.setCreateTime(directBaseInfo.getCreateTime());
                directSignInfoService.save(directSignInfoTmp);
                // 将正式表数据更新到历史表
                bizHisService.saveBizHisNew(directSignInfoTmp, directSignInfo, user, org.getInstId(), directSignInfoBizTmp.getBizOpr(), lastUpddt, directSignInfoBizTmp.getOprMenuId(), directSignInfoBizTmp.getOprMenuName(), directSignInfoBizTmp.getRealId(), mchntCd, null, null);
                // 商户影像信息
                List<Object> bMchntImageInfoTmps = tableDataMap.get("T_B_MCHNT_IMAGE_INFO");
                List<BizTmp> bMchntImageInfoBizTmps = bizTmpDataMap.get("T_B_MCHNT_IMAGE_INFO");
                BizTmp bMchntImageInfoBizTmp = bMchntImageInfoBizTmps.get(0);
                List<String> bizOprList = new ArrayList<>();
                List<String> realIdList = new ArrayList<>();
                for (Object bizTmp : bMchntImageInfoBizTmps) {
                    BizTmp bizTmp1 = (BizTmp) bizTmp;
                    bizOprList.add(bizTmp1.getBizOpr());
                    realIdList.add(bizTmp1.getRealId());
                }
                // 审核通过，将临时表数据同步到正式表
                for (Object bMchntImageInfoTmp : bMchntImageInfoTmps) {
                    UpmpBMchntImageInfo img = (UpmpBMchntImageInfo) bMchntImageInfoTmp;
                    img.setOprId(directBaseInfo.getOprId());
                    img.setCreateTime(directBaseInfo.getCreateTime());
                    directMchntImageInfoService.save(img);
                }
                // 将正式表数据更新到历史表
                bizHisService.saveBizHisList(bMchntImageInfoTmps, bMchntImageInfos, user, org.getInstId(), bizOprList, lastUpddt, bMchntImageInfoBizTmp.getOprMenuId(), bMchntImageInfoBizTmp.getOprMenuName(), realIdList, mchntCd, null, null);
                // 商户清算信息
                List<Object> directClearInfoTmps = tableDataMap.get("T_B_DIRECT_MCHNT_CLEAR_INFO");
                List<BizTmp> directClearInfoBizTmps = bizTmpDataMap.get("T_B_DIRECT_MCHNT_CLEAR_INFO");
                DirectMchntClearInfo directClearInfoTmp = (DirectMchntClearInfo) directClearInfoTmps.get(0);
                BizTmp directClearInfoBizTmp = directClearInfoBizTmps.get(0);
                // 审核通过，将临时表数据同步到正式表
                directClearInfoTmp.setOprId(directBaseInfo.getOprId());
                directClearInfoTmp.setCrtOrgId(directBaseInfo.getCrtOrgId());
                directClearInfoTmp.setCreateTime(directBaseInfo.getCreateTime());
                directClearInfoService.save(directClearInfoTmp);
                // 将正式表数据更新到历史表
                bizHisService.saveBizHisNew(directClearInfoTmp, directClearInfo, user, org.getInstId(), directClearInfoBizTmp.getBizOpr(), lastUpddt, directClearInfoBizTmp.getOprMenuId(), directClearInfoBizTmp.getOprMenuName(), directClearInfoBizTmp.getRealId(), mchntCd, null, null);
            }
        }
    }
}
